/*
 * Created by JFormDesigner on Mon Dec 21 20:26:46 CST 2020
 */

package cn.fourhundredandeighteen.view.StudentView.StudentCourseInfo;

import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.utill.ViewUtill;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.StudentView.StudentFunction.StudentCourswInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;


/**
 * @author root
 */
public class studentCourseInfo extends JPanel {
    public static Long newText;
    public StudentCourswInfo studentCourswInfo = new StudentCourswInfo();
    public StudentController studentController = new StudentController();
    public Long StudentSession;
    public JScrollPane studentCourswInfo1;
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JButton button1;
    private JButton button2;

    public studentCourseInfo(Long StudentSession) {
        this.StudentSession = StudentSession;
        initComponents();
        getdata();
    }

    public void getdata() {
        studentCourswInfo1 = studentCourswInfo.getStudentCourswInfo(StudentSession);
        this.add(studentCourswInfo1);
        this.updateUI();
    }

    private void thisPropertyChange(PropertyChangeEvent e) {
        // TODO add your code here
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        try {
            Result result = studentController.cancelCourse(Long.valueOf(newText), StudentSession);

            if (result.getMassage() == "SUCCESS") {
                this.remove(studentCourswInfo1);

                JOptionPane.showConfirmDialog(null, "退课成功", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                getdata();
            } else {
                JOptionPane.showConfirmDialog(null, "退课失败", "请输入正确课程号", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (NumberFormatException error) {
            JOptionPane.showConfirmDialog(null, "退课失败", "请输入课程号", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void button2ActionPerformed(ActionEvent e) {
        try {
            JScrollPane jScrollPane = ViewUtill.viewCourseScoreTable(StudentSession, Long.valueOf(newText));
            JOptionPane.showMessageDialog(null, jScrollPane, "成绩查询", JOptionPane.DEFAULT_OPTION);
        } catch (NumberFormatException n) {
            JOptionPane.showMessageDialog(null, "请输入课程号", "成绩查询", JOptionPane.DEFAULT_OPTION);
        }
        // TODO add your code here

    }

    private void thisComponentShown(ComponentEvent e) {
        // TODO add your code here
        System.out.println(1);
    }

    private void button1FocusLost(FocusEvent e) {
        // TODO add your code here
        System.out.println(1);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        button1 = new Button(Button.CANCELBUTTON);
        button2 = new Button();

        //======== this ========
        addPropertyChangeListener(e -> thisPropertyChange(e));
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                thisComponentShown(e);
            }
        });
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));

            //---- button1 ----
            button1.setText("\u9000\u8bfe");
            button1.addActionListener(e -> button1ActionPerformed(e));
            button1.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    button1FocusLost(e);
                }
            });
            panel1.add(button1);

            //---- button2 ----
            button2.setText("\u6210\u7ee9\u67e5\u8be2");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);
        }
        add(panel1, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables

}
